package com.xzw.cameratest.push;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.android.business.adapter.DataAdapterImpl;
import com.android.business.entity.LoginType;
import com.android.business.entity.UserInfo;
import com.android.business.exception.BusinessException;
import com.xzw.cameratest.common.CommonInfo;
import com.xzw.cameratest.common.Config;
import com.xzw.cameratest.utils.SPUtils;


/**
 * 作者：ZXD
 * 日期：2020/7/22
 * 描述：
 **/
public class ReconnectService extends Service {

    private boolean mLoginSuccess;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new ReconnectBinder();
    }

    public void reconnectServe() {
        mLoginSuccess = false;
        final String mUuid = SPUtils.getString(Config.UUID, null);
        final String username = SPUtils.getString(Config.USERNAME, null);
        final String password = SPUtils.getString(Config.PASSWORD, null);
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!mLoginSuccess) {
                    UserInfo userInfo = null;
                    try {
                        userInfo = DataAdapterImpl.getInstance().login(username, password, "1", mUuid, LoginType.TYPE_APP_CHINESE, 0);
                    } catch (BusinessException e) {
                        e.printStackTrace();
                    }
                    CommonInfo.getInstance().setUserInfo(userInfo);
                    mLoginSuccess = true;
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    public class ReconnectBinder extends Binder {
        public ReconnectService getService() {
            return ReconnectService.this;
        }
    }
}
